/*
 * Created by JFormDesigner on Thu Jun 08 19:50:04 CST 2023
 */

package gui.admin;

import dao.PurchaseDao;
import dao.SaleDao;
import dao.SupplierDao;
import entity.ProductCategory;
import entity.Purchase;
import entity.Sale;
import entity.Supplier;
import util.OrderNoUtil;
import util.PurchaseNoUtil;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.time.LocalDate;
import java.util.List;
import javax.swing.*;

/**
 * @author Liubuzhu
 */
public class AddPurchaseFrame extends JFrame {
    SupplierDao supplierDao = new SupplierDao();
    public AddPurchaseFrame() throws Exception {
        initComponents();
    }

    private void initComponents() throws Exception {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off
        label1 = new JLabel();
        label2 = new JLabel();
        textField = new JTextField();
        button1 = new JButton();
        comboBox1 = new JComboBox();

        List supplierList = supplierDao.getAllSuppliers();//获取所有的供货商，添加到列表中
        for (int i = 0; i < supplierList.size(); i++) {
            Supplier supplier = (Supplier) supplierList.get(i);//获取第i个供货商对象
            //给下拉框添加选项
            comboBox1.addItem(new ProductCategory(supplier.getSupplierId(),supplier.getSupplierName()){
                public String toString(){
                    String supplierName = supplier.getSupplierName();
                    return supplierName;
                }
            });
        }

        //======== this ========
        setTitle("\u9009\u62e9\u4f9b\u8d27\u5546");
        setPreferredSize(new Dimension(510, 420));
        setVisible(true);
        var contentPane = getContentPane();
        contentPane.setLayout(null);

        //---- label1 ----
        label1.setText("\u8bf7\u9009\u62e9\u4f9b\u8d27\u5546\uff1a");
        label1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label1);
        label1.setBounds(50, 30, 150, 60);

        //---- label2 ----
        label2.setText("请输入经手人姓名：");
        label2.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(label2);
        label2.setBounds(50, 150, 150, 60);



        //---- button1 ----
        button1.setText("\u786e\u5b9a");
        button1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(button1);
        button1.setBounds(290, 300, 100, 50);

        button1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                PurchaseDao purchaseDao = new PurchaseDao();

                String purchaseNo = PurchaseNoUtil.randomOrderCode();//获取随机订单编号
                LocalDate currentDate = LocalDate.now();//获取此时日期
                String purchaseDate = currentDate.toString();
                String handlerName = textField.getText();
                int index = comboBox1.getSelectedIndex();
                Supplier supplier = (Supplier) supplierList.get(index);
                Purchase purchase = new Purchase(purchaseNo,supplier.getSupplierId(),handlerName,0.0F,purchaseDate);
                try {
                    purchaseDao.addPurchase(purchase);//将供货单信息单添加进数据库
                    dispose();
                    AddPurchaseDetailFrame addPurchaseDetailFrame = new AddPurchaseDetailFrame(purchase);
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }
        });

        //---- comboBox1 ----
        comboBox1.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(comboBox1);
        comboBox1.setBounds(140, 95, 250, 50);

        //---- textField ----
        textField.setFont(new Font("Microsoft YaHei UI", Font.PLAIN, 16));
        contentPane.add(textField);
        textField.setBounds(140, 215, 250, 50);

        {
            // compute preferred size
            Dimension preferredSize = new Dimension();
            for(int i = 0; i < contentPane.getComponentCount(); i++) {
                Rectangle bounds = contentPane.getComponent(i).getBounds();
                preferredSize.width = Math.max(bounds.x + bounds.width, preferredSize.width);
                preferredSize.height = Math.max(bounds.y + bounds.height, preferredSize.height);
            }
            Insets insets = contentPane.getInsets();
            preferredSize.width += insets.right;
            preferredSize.height += insets.bottom;
            contentPane.setMinimumSize(preferredSize);
            contentPane.setPreferredSize(preferredSize);
        }
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables  @formatter:off
    private JLabel label1;
    private JLabel label2;
    private JTextField textField;
    private JButton button1;
    private JComboBox comboBox1;
    // JFormDesigner - End of variables declaration  //GEN-END:variables  @formatter:on
    public static void main(String[] args) throws Exception {
        AddPurchaseFrame addPurchaseFrame = new AddPurchaseFrame();
    }
}
